<route lang="json5" type="page">
{
  style: { navigationBarTitleText: '动态视图' },
}
</route>

<template>
  <IBizViewShell
    v-if="isLoaded"
    :context="viewData.context"
    :params="viewData.params"
    :viewId="viewData.viewConfig.id"
    :key="viewData.viewConfig.codeName"
    :state="{ srfnav: viewData.srfnav }"
  ></IBizViewShell>
</template>

<script setup lang="ts">
import { IBizContext } from '@ibiz-template/core'
import { parseRouteViewData } from '@/utils'
import { IRouteViewData } from '@/interface'

const isLoaded = ref(false)

const viewData = ref<IRouteViewData>({})

/**
 * 初始化视图数据
 */
const initViewData = async () => {
  try {
    const _viewData = await parseRouteViewData()
    viewData.value = {
      ..._viewData,
      context: IBizContext.create(_viewData.context),
    }
    isLoaded.value = true
  } catch (error) {
    uni.redirectTo({
      url: '/pages/404',
    })
  }
}

/**
 * 销毁上下文
 */
const destroyContext = () => {
  if (viewData.value.context) {
    const { context } = toRaw(viewData.value)
    if (context) context.destroy()
  }
}

/**
 * 渲染完成
 */
onReady(() => {
  initViewData()
})

/**
 * 页面卸载
 */
onUnload(() => {
  destroyContext()
})
</script>

<style lang="scss" scoped>
//
</style>
